package com.itheima.doublylinkedlist;

import java.util.Iterator;

/**
 * @FileName DoubleLinkedListSentinel
 * @Description
 * @Author Sword
 * @date 2025-03-05
 **/
public class DoublyLinkedListSentinel implements Iterable {

    @Override
    public Iterator iterator() {
        return new Iterator() {

            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Object next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    private static class Node {
        Node prev;  //上一个节点指针
        int value;  //值
        Node next;  //下一个节点指针

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    private Node head;  //头哨兵

    private Node tail;  //尾哨兵

    public DoublyLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 888, null);
        head.next = tail;
        tail.prev = head;
    }


    /**
     * 根据索引查找节点
     *
     * @param index
     * @return
     */
    public Node findNode(int index) {
        int i = -1;

        for (Node p = head; p != tail; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }

        return null;
    }

    /**
     * 向双向链表的第一个索引元素插入值
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 删除双向链表的第一个索引元素
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 向双向链表末尾插入元素
     *
     * @param value
     */
    public void addLast(int value) {
        Node last = tail.prev;
        Node added = new Node(last, value, tail);
        last.next = added;
        tail.prev = added;
    }

    /**
     * 删除双向链表末尾的元素
     */
    public void removeLast() {
        Node removed = tail.prev;
        if (removed == head) {
            throw illegalIndex(0);
        }
        Node prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 双向链表插入节点
     *
     * @param index
     * @param vaule
     */
    public void insert(int index, int vaule) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node next = prev.next;
        Node inserted = new Node(prev, vaule, next);
        prev.next = inserted;
        next.prev = inserted;

    }

    /**
     * 双向链表根据索引删除节点
     *
     * @param index
     */
    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node removed = prev.next;

        if (removed == tail) {
            throw illegalIndex(index);

        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }


    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法", index));
    }
}
